var params = new Object();
var tableNames;
var filename;
var fileId;
var attachmentParamterId;
$(function () {
    //获取父页面的值
    setValue()
    //初始化页面
    var grid = $("#tt");
    initGridExcelSheet(grid);
});

//获取父页面的值
function setValue() {
    var name, value;
    var str = decodeURI(decodeURI(window.location.href));
    var num = str.indexOf("?")
    str = str.substr(num + 1); //取得所有参数   stringvar.substr(start [, length ]
    // 设值
    var arr = str.split("&"); //各个参数放到数组里
    for (var i = 0; i < arr.length; i++) {
        num = arr[i].indexOf("=");
        if (num > 0) {
            name = arr[i].substring(0, num);
            value = arr[i].substr(num + 1);
            if ("filename" == name) {
                filename = value;
            }
            if ("tableNames" == name) {
                tableNames = value;
            }
            if ("fileId" == name) {
                fileId = value;
            }
            if ("attachmentParamterId" == name) {
                attachmentParamterId = value;
            }

        }
    }
}

function initGridExcelSheet(grid) {
    var columns = [
        {
            field: "id", title: "ID", hidden: true,
            align: "left", sortable: false, resizable: true, width: 120
        },
        {
            field: "excelSeheetName",
            title: "工作簿名称",
            align: "left",
            halign: "center",
            sortable: false,
            resizable: true,
            width: 250
        },
        {
            field: "excelSeheetId",
            title: "工作簿id",
            align: "left",
            halign: "center",
            sortable: false,
            resizable: true,
            hidden: true,
            width: 10
        }
    ];

    initGridWithCheckBox(grid, params, "", columns, "#tb", "id");
}

/**
 * 初始化 DataGrid控件信息带checkbox多选不分页 (默认选中)
 *
 * @param grid
 */
function initGridWithCheckBox(grid, params, url, columns, toolbar,
                              fieldId,singleSelect) {
    var f = false;
    if (singleSelect) {
        f = true;
    }
    // 初始化 DataGrid控件信息
    grid.datagrid({
        url : "",
        title : "",
        toolbar : toolbar,
        fit:false,
        loadMsg : "",
        queryParams : {},
        singleSelect : f,
        nowrap : true,
        striped : true,
        idField : fieldId,
        remoteSort : false,
        frozenColumns : [ [ {
            field : 'check',
            checkbox : true
        } ] ],
        columns : [ columns ],
        onLoadSuccess : function(data) {
            grid.datagrid('clearSelections');
        },
        onLoadError : function() {
            $("#loading").hide();
        }
    });
    getData(grid, params, url);
}


/**
 * grid不分页查询
 *
 * @param grid
 * @param params
 * @param url
 */
function getData(grid, params1, url) {

    params.fileId =fileId;
   var url = '/common/excelHandleIndex/getExcelSheet';

    //ajax提交
    $.ajax({
        type: "POST",
        url: url,
        dataType: "json",
        data: params,
        success: function callback(data) {
            var analyMsg = data;
            if (analyMsg.code != '0000') {
                $.messager.alert("操作提示", analyMsg.msg, "error", function () {
                });
            } else {
                if (analyMsg.returnObject.sheetList.length > 0) {
                    //清空原始数据
                    grid.datagrid('loadData', {total: 0, rows: []});
                    grid.datagrid("loadData", analyMsg.returnObject.sheetList);
                    displayMsg(grid);
                } else {
                    grid.datagrid('loadData', {
                        total: 0,
                        rows: []
                    });
                }

                $("#fileNamed").val(filename);

            }
        },
        error: function (data, textstatus) {
        }
    });

}


function checkInput() {
    //检查参数是否合法
    var rows = $('#tt').datagrid('getSelections');
    if (rows.length == 0) {
        $.alert("default", "info", "请选择Excel工作簿");
        return false;
    }

    var titleIndex = $("#titleIndex").val();

    if (titleIndex == "") {
        $.alert("default", "info", "Excel标题读取行不能为空,请填写具体读取Excel标题行");
        return false;
    }

    if (!(/^[0-9]*$/.test(titleIndex))) {
        $.messager.alert('提示', 'Excel标题读取行只能为数字。', 'info', function () {
        });
        return false;
    }
    return true;
}


/**
 * 取消
 */
function btnReset() {
    $("#file").val("");
    closeLayer();
}


//获取Excel标题并生成对应关系
function btn_import_set() {

    // 检查参数是否合法
    if (!checkInput()) {
        return;
    }

    var titleIndex = $("#titleIndex").val();
    titleIndex = titleIndex -1;
    var rows = $('#tt').datagrid('getSelections');

    var sheetName ="";
    var sheetIndex= "";
    for(var  i = 0 ;i< rows.length ;i++){
        sheetName += rows[i].excelSeheetName + "|" + rows[i].excelSeheetId +"," ;
        sheetIndex += rows[i].excelSeheetId +",";
    }
    var url = '/common/excelHandleIndex/excelRelateIndex?tableNames=' + tableNames + '&fileId=' + fileId +
        '&titleIndex=' + titleIndex + '&sheetName=' + sheetName + '&sheetIndex=' + sheetIndex + '&attachmentParamterId='+ attachmentParamterId ;
    parent.layer.open({
        type: 2,
        title: '',
        shadeClose: false,
        shade: 0.3,
        area: ['800px', '500px'],
        content: url,
        maxmin: false
    });

}

